//
//  OMSettingViewController.m
//  Weibbo
//
//  Created by 李 林刚 on 13-6-17.
//  Copyright (c) 2013年 Omnivores. All rights reserved.
//

#import "OMSettingViewController.h"
#import "SettingViewAdapter.h"
#import "ASIFormDataRequest.h"
#import "JSONKit.h"

@interface OMSettingViewController ()<TableViewAction>

@property (nonatomic, retain)UITableView   *setTableView;
@property (nonatomic, retain)SettingViewAdapter  *adapter;
@property (nonatomic, retain)NSMutableArray  *list;

@end

@implementation OMSettingViewController
@synthesize setTableView;
@synthesize adapter;
@synthesize list;

-(void)dealloc
{
    UIRelease(adapter);
    UIRelease(setTableView);
    UIRelease(list);
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UINavigationBar *navagationBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44.0)];
    [navagationBar setBarStyle:UIBarStyleBlack];
    [self.view addSubview:navagationBar];
    [navagationBar release];
    
    UILabel  *shareNaeLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44.0)];
    shareNaeLab.backgroundColor = [UIColor clearColor];
    [shareNaeLab setText:@"设置"];
    [shareNaeLab setTextAlignment:NSTextAlignmentCenter];
    [shareNaeLab setFont:[UIFont boldSystemFontOfSize:20]];
    [shareNaeLab setTextColor:[UIColor whiteColor]];
    [self.view addSubview:shareNaeLab];
    
    UIButton  *CancelButton = [[UIButton alloc]initWithFrame:CGRectMake(15, 10, 12, 22)];
    [CancelButton addTarget:self action:@selector(PopViewController) forControlEvents:UIControlEventTouchUpInside];
    [CancelButton setImage:[UIImage imageNamed:@"setting_back"] forState:UIControlStateNormal];
    [self.view addSubview:CancelButton];
    [CancelButton release];
    

    NSArray *firstArr = [NSArray arrayWithObjects:@"新浪微博",@"QQ空间",@"官方方反馈群", nil];
    NSArray *secondArr = [NSArray arrayWithObjects:@"检查更新",@"支持",@"当前版本", nil];
    list = [[NSMutableArray alloc]initWithObjects:firstArr,secondArr,nil];
    
    adapter = [[SettingViewAdapter alloc]initWithList:list andAdapter:self];
    setTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 44, SCREEN_WIDTH, SCREEN_HEIGHT-44) style:UITableViewStyleGrouped];
    [setTableView setTableAdapter:adapter];
    [setTableView setBackgroundView:nil];
    [setTableView setBackgroundColor:UIWhiteColor];
    [self.view addSubview:setTableView];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//界面返回
-(void)PopViewController
{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark  ---TableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==1) {
        switch (indexPath.row) {
            case 0://检查更新//代码参考http://blog.csdn.net/wave_1102/article/details/7463697
            {
                NSString *version = @"";
                NSURL *url = [NSURL URLWithString:@"http://itunes.apple.com/app/remote/id284417350?mt=8"];
                ASIFormDataRequest *versionRequest = [ASIFormDataRequest requestWithURL:url];
                [versionRequest setRequestMethod:@"GET"];
                [versionRequest setDelegate:self];
                [versionRequest setTimeOutSeconds:150];
                [versionRequest addRequestHeader:@"Content-Type" value:@"application/json"];
                [versionRequest startSynchronous];
                
                //Response string of our REST call
                NSString* jsonResponseString = [versionRequest responseString];
                
                NSDictionary *loginAuthenticationResponse = [jsonResponseString objectFromJSONString];
                
                NSArray *configData = [loginAuthenticationResponse valueForKey:@"results"];
                
                for (id config in configData)
                {
                    version = [config valueForKey:@"version"];
                }
                //Check your version with the version in app store
                NSString *clientVersion = [[NSBundle mainBundle]objectForInfoDictionaryKey:@"CFBundleVersion"];
                if (![version isEqualToString:clientVersion])
                {
                    UIAlertView *createUserResponseAlert = [[UIAlertView alloc] initWithTitle:@"New Version!!" message: @"A new version of app is available to download" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: @"Download", nil];
                    [createUserResponseAlert show]; 
                    [createUserResponseAlert release];
                    [tableView reloadData];
                }
            }
                break;
            default:
            {
                NSString *url = [NSString stringWithFormat:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%d",490062954];
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
            }
                break;
        }
        
    }

}


@end
